package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.BargainVO;
import com.burgeon.backend.SHOPBACKEND.entity.Goods;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendBargainMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGoodsMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackendBargainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BackendBargainServiceImpl implements BackendBargainService {

    @Autowired
    private BackendBargainMapper backendBargainMapper;
    @Autowired
    private BackendGoodsMapper backendGoodsMapper;

    @Override
    public Goods getGoodsById(String gid) {
        return backendGoodsMapper.getGoodsById(gid);
    }

    @Override
    public List<BargainVO> getListData(String account) {
        return backendBargainMapper.getBargainList(account);
    }

    @Override
    public void delData(String id) {
        backendBargainMapper.delbargain(id);
    }

    @Override
    public void upState(String id, String state) {
        backendBargainMapper.upBargainState(id, state);
    }

    @Override
    public void createBargain(BargainVO bargainVO) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd" + " " + "HH:mm:ss");
        String now = sdf.format(new Date());
        bargainVO.setCreateTime(now);
        bargainVO.setGid(backendGoodsMapper.getGidByCode(bargainVO.getCode(),bargainVO.getAccount()));

        backendBargainMapper.createBargainData(bargainVO);
        backendGoodsMapper.upGoodsState(bargainVO.getGid(),"N");

    }

    @Override
    public void upUnderTime() {
        List<BargainVO> bargainVOS = backendBargainMapper.getBargainAll();
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDate = sf.format(date);
        Date d = null;
        for (BargainVO bargainVO:bargainVOS) {
            try {
                d = sf.parse(newDate);
                System.err.println(bargainVO.getUnderTime());
                if(bargainVO.getUnderTime() != null) {
                    if (sf.parse(bargainVO.getUnderTime()).before(d)) {
                        backendBargainMapper.upBargainState(bargainVO.getId(), "N");
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
    }

}
